<?php

class CmpServicesController extends BaseController
{

	/**
	 * AJAX设为默认
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionAjaxSetDefault($id)
	{
		$model=$this->loadModel($id);

		//取销原来的默认客服
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$set = array('zcs_default'=>1);
		CmpServices::model()->updateAll($set,$criteria);

		$model->zcs_default = 2;
		if($model->save()){
		    //成功 
		   	echo 1;
		    exit;  
		}else{ 
		    //失败
		    echo '对不起，产品审核操作失败，请稍后再试！';  
		    exit;  
		}
	}
	
	/**
	 * AJAX,链接排序上移
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUplist($id)
	{
		$model=$this->loadModel($id);
		
		
		$list = $model->zcs_listorder;
		//查找比它小的一个
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$criteria->compare('zcs_listorder','<'.$list);
		$criteria->order = "zcs_listorder DESC";
		$prev =  CmpServices::model()->find($criteria);
	
		if(isset($_GET['ajax'])){
			if(empty($prev)){
				echo "已经是最前面了！";exit;
			}
			//互换排序ID
			$model->zcs_listorder = $prev->zcs_listorder;
			$prev->zcs_listorder = $list;
			
			if($model->save()&&$prev->save()){
				//成功跳转
				echo 1;
				exit;
			}else{
				//失败跳转
				echo '保存失败，请稍后再试！';
				exit;
			}
		}else{
			if(empty($prev)){
				//失败跳转  
			    $this->render('../index/error',array('message'=>'对不起，操作失败，已经是最前面了！'));  
			    exit;  
			}
			//互换排序ID
			$model->zcs_listorder = $prev->zcs_listorder;
			$prev->zcs_listorder = $list;
				
			if($model->save()&&$prev->save()){
			    //成功跳转  
			    $this->redirect(Yii::app()->request->urlReferrer);
			}
		}
	}
	
	/**
	 * AJAX,链接排序下移
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionDownlist($id)
	{
		$model=$this->loadModel($id);
	
		$list = $model->zcs_listorder;
		//查找比它的下一个
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Company::model()->getCmpUserId());
		$criteria->compare('zcs_listorder','>'.$list);
		$criteria->order = "zcs_listorder ASC";
		$next =  CmpServices::model()->find($criteria);
	
		if(isset($_GET['ajax'])){
			if(empty($next)){
				echo "已经是最后面了！";exit;
			}
			//互换排序ID
			$model->zcs_listorder = $next->zcs_listorder;
			$next->zcs_listorder = $list;
		
			if($model->save()&&$next->save()){
				//成功跳转
				echo 1;
				exit;
			}else{
				//失败跳转
				echo '保存失败，请稍后再试！';
				exit;
			}
		}else{
			if(empty($next)){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，操作失败，已经是最后面了！'));
				exit;
			}
			//互换排序ID
			$model->zcs_listorder = $next->zcs_listorder;
			$next->zcs_listorder = $list;
			
			if($model->save()&&$next->save()){
				//成功跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
	}
	
	/**
	 * 删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if($this->loadModel($id)->delete()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}

	/**
	 * 客服管理.
	 */
	public function actionAdmin()
	{
		$model=new CmpServices('search');
		$model->unsetAttributes();  // clear any default values
		$model->zm_id = Company::model()->getCmpUserId();
		
		//如果是修改的时候实例化
		if(isset($_GET['id'])){
			$newModel = $this->loadModel($_GET['id']);
		}else{
			$newModel = new CmpServices;
			$newModel->zm_id = Company::model()->getCmpUserId();
		}
		
		//提交保存数据
		if(isset($_POST['CmpServices']))
		{
			$newModel->attributes=$_POST['CmpServices'];
			if($newModel->isNewRecord){
				//取销原来的默认客服
				$criteria=new CDbCriteria;
				$criteria->compare('zm_id',Company::model()->getCmpUserId());
				$criteria->order = "zcs_listorder DESC";
				$last = CmpServices::model()->find($criteria);
				$newModel->zcs_listorder = !empty($last)?$last->zcs_listorder+1:1;
			}
			
			if($newModel->save()){
			    //成功跳转  
			    $this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		
		if(isset($_GET['CmpServices']))
			$model->attributes=$_GET['CmpServices'];

		$this->render('admin',array(
			'model'=>$model,
			'newModel'=>$newModel,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=CmpServices::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-services-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
